package com.cloudkinto.service.sowWall.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.bo.sowWall.SowWallAddBo;
import com.cloudkinto.bo.sowWall.SowWallQueryBo;
import com.cloudkinto.common.BeanConvert;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.dao.SowWallDao;
import com.cloudkinto.entity.SowWallDo;
import com.cloudkinto.service.sowWall.SowWallService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 * 播种墙 service实现类
 * </p>
 *
 * @author long
 * @since 2024-12-03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SowWallServiceImpl extends ServiceImpl<SowWallDao, SowWallDo> implements SowWallService {


    @Override
    public int add(SowWallAddBo req) {
//        SowWallDo entityDo = new SowWallDo();
//        BeanUtils.copyProperties(req, entityDo);
        SowWallDo entityDo = BeanConvert.INSTANCE.sowWallAddBoToSowWallDo(req);
        entityDo.setInUse(Boolean.FALSE);
        SowWallDo exist = baseMapper.selectOne(Wrappers.<SowWallDo>lambdaQuery().eq(SowWallDo::getSowCode, req.getSowCode()).last(" limit 1"));
        if (Objects.nonNull(exist)) {
            throw new BizException(SysConstant.SowWall_Code_Exists, req.getSowCode());
        }
        return baseMapper.insert(entityDo);
    }


    @Override
    public int delete(Long id) {
        SowWallDo sowWallDo = baseMapper.selectById(id);
        if (sowWallDo.getInUse()) {
            throw new BizException(SysConstant.SowWall_In_Use);
        }
        return baseMapper.deleteById(id);
    }

    @Override
    public PageResult getListPage(SowWallQueryBo bo) {
        IPage<SowWallDo> page = new Page<>(bo.getCurrent(), bo.getPageSize());
        page = this.page(page, this.queryBuild(bo));
        return new PageResult<>(page.getRecords(), (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
    }

    private LambdaQueryWrapper<SowWallDo> queryBuild(SowWallQueryBo bo) {
        LambdaQueryWrapper<SowWallDo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(bo.getSowCode()), SowWallDo::getSowCode, bo.getSowCode());
        lqw.eq(Objects.nonNull(bo.getWhId()), SowWallDo::getWhId, bo.getWhId());
        lqw.eq(Objects.nonNull(bo.getTenantId()), SowWallDo::getTenantId, bo.getTenantId());
        lqw.eq(Objects.nonNull(bo.getInUse()), SowWallDo::getInUse, bo.getInUse());
        lqw.eq(Objects.nonNull(bo.getTierNum()), SowWallDo::getTierNum, bo.getTierNum());
        lqw.eq(Objects.nonNull(bo.getColumnNum()), SowWallDo::getColumnNum, bo.getColumnNum());
        lqw.last(
                StrUtil.isNotBlank(bo.getSortName()),
                "order by " + bo.getSortName() + " " + bo.getSortOrder()
        );
        return lqw;
    }


}
